package com.example.homework.homework02;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

// 惩罚类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Punishment {
    private int leftCount; // 剩余抄写的次数
    private String content; // 抄写的内容

    public void write() {
        // 定义遍历统计每个线程的次数
        int count = 0;
        // 获取当前线程名称
        String name = Thread.currentThread().getName();
        while (true) {
            synchronized (this) {
                if (leftCount > 0) {
                    // 说明还没有抄完继续
                    System.out.println(name + "线程抄写了一遍" + content + "，剩余" + --leftCount + "次");
                    count++;
                } else {
                    // 抄完了，跳出循环
                    break;
                }
            }
        }
        System.out.println(name + "线程抄写了" + count + "次");
    }
}
